const newmap=new WeakMap()

function debounce(fn, delay) {
  let timer = null;
  return function (...args) {
    clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(this, args);
    }, delay);
  };
}

const ob=new ResizeObserver(entries=>{
  for (const entry of entries) {
    const handler = newmap.get(entry.target);
    let width, height;
    if (entry.borderBoxSize) {
      const contentBoxSize = Array.isArray(entry.borderBoxSize)
        ? entry.borderBoxSize[0]
        : entry.borderBoxSize[0];
      width = contentBoxSize.inlineSize;
      height = contentBoxSize.blockSize;
    } else {
      width = entry.contentRect.width;
      height = entry.contentRect.height;
    }
    
    handler({ width, height });
  }

})

export default {
  mounted(el, binding) { 
ob.observe(el);
    newmap.set(el,binding.value);

  },
  unmounted() { 
    ob.unobserve(el);
  }
}